//
//  NSDate_Extension.swift
//  WB
//
//  Created by leador on 17/5/9.
//  Copyright © 2017年 袁满. All rights reserved.
//

import Foundation

extension NSDate {
    
    class func createDateString(createDateStr:String)->String {
    
        let fmt = DateFormatter()
        fmt.locale = Locale(identifier:"en")
        fmt.dateFormat = "EEE MM dd HH:mm:ss Z yyyy"
        
        guard let createDate = fmt.date(from: createDateStr) else {
            return ""
        }
        
        let nowDate = Date()
        
        let interval = Int(nowDate.timeIntervalSince(createDate))
        
        if interval < 60 {
            return "刚刚"
        }
        
        if interval < 60*60 {
            return "\(interval/60)分钟前"
        }
        
        if interval < 60*60*24 {
            return "\(interval/(60*60))小时前"
        }
        
        let calendar = NSCalendar.current
        
        if calendar.isDateInYesterday(createDate) {
            fmt.dateFormat = "昨天 HH:mm"
            return fmt.string(from: createDate)
        }
        
        let components = calendar.dateComponents([.year], from: createDate, to: nowDate)
        
        if components.year! < 1 {
            fmt.dateFormat = "MM-dd HH:mm:ss"
            return fmt.string(from: createDate)
        }
        
        fmt.dateFormat = "yyyy-MM-dd HH:mm:ss"
        return fmt.string(from: createDate)
        
        
    }
    
}
